package com.easy.controller;

import com.easy.bean.MessageBean;
import com.easy.service.MessageService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取用户消息列表
     */
    @GetMapping("/list")
    public Result getMessages(
            @RequestParam(required = true) Integer receiverId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        if (receiverId == null) {
            return Result.fail(400,"接收者ID（receiverId）不能为空");
        }
        // 调用服务层方法
        return Result.success(messageService.getMessagesByReceiverId(receiverId, pageNum, pageSize));
    }

    /**
     * 按类型获取用户消息列表
     */
    @GetMapping("/listByType")
    public Result getMessagesByType(@RequestParam Integer receiverId,
                                    @RequestParam String msgType,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit) {
        Page messages = messageService.getMessagesByTypeAndReceiver(receiverId, msgType, page, limit);
        return Result.success(messages);
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unreadCount")
    public Result getUnreadCount(@RequestParam Integer receiverId) {
        int count = messageService.getUnreadCount(receiverId);
        return Result.success(count);
    }

    /**
     * 按类型获取未读消息数量
     */
    @GetMapping("/unreadCountByType")
    public Result getUnreadCountByType(@RequestParam Integer receiverId,
                                       @RequestParam String msgType) {
        int count = messageService.getUnreadCountByType(receiverId, msgType);
        return Result.success(count);
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/markAsRead")
    public Result markAsRead(@RequestBody List<Integer> ids) {
        boolean success = messageService.markMessagesAsRead(ids);
        return success ? Result.success("标记成功") : Result.fail(500, "标记失败");
    }

    /**
     * 标记所有消息为已读
     */
    @PostMapping("/markAllAsRead")
    public Result markAllAsRead(@RequestParam Integer receiverId) {
        boolean success = messageService.markAllMessagesAsRead(receiverId);
        return success ? Result.success("标记成功") : Result.fail(500, "标记失败");
    }

    /**
     * 按类型标记消息为已读
     */
    @PostMapping("/markAsReadByType")
    public Result markAsReadByType(@RequestParam Integer receiverId,
                                   @RequestParam String msgType) {
        boolean success = messageService.markMessagesAsReadByType(receiverId, msgType);
        return success ? Result.success("标记成功") : Result.fail(500, "标记失败");
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteMessage(@PathVariable("id") Integer id) {
        System.out.println(id);
        int success = messageService.deleteMessage(id);
        System.out.println(success);
        return success>0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    /**
     * 获取消息详情
     */
    @GetMapping("/detail")
    public Result getMessageDetail(@RequestParam Integer id) {
        MessageBean message = messageService.getMessageById(id);
        if (message != null) {
            return Result.success(message);
        } else {
            return Result.fail(404, "消息不存在");
        }
    }
}
